import { UIConfig } from "./UIManager";

export class ViewParams {
    uuid: string = null!;
    prefabPath: string = null!;
    params: any = null!;
    valid: boolean = true;
    node: cc.Node = null!;
}

export class LayerUI extends cc.Node {

    protected ui_nodes = new Map<string, ViewParams>();
    protected ui_cache = new Map<string, ViewParams>();

    protected paramsList = []
    constructor(name: string) {
        super(name);

        let widget: cc.Widget = this.addComponent(cc.Widget);
        widget.isAlignLeft = widget.isAlignRight = widget.isAlignTop = widget.isAlignBottom = true;
        widget.left = widget.right = widget.top = widget.bottom = 0;
        widget.alignMode = cc.Widget.AlignMode.ALWAYS;
        widget.enabled = true
    }

    protected getUuid(prefabPath: string): string {
        var uuid = `${this.name}_${prefabPath}`;
        return uuid.replace(/\//g, "_");
    }

    add(config: UIConfig) {
        let path = config.prefab;
        let uuid = this.getUuid(path);
        let param = this.ui_nodes.get(uuid);
        if (param && param.valid) {
            cc.warn(path + "重复加载");
            return "";
        }

        if (param == null) {
            param = new ViewParams();
            param.uuid = uuid;
            param.prefabPath = path;
            this.ui_nodes.set(param.uuid, param);
        }

        param.valid = true;
        this.load(param)
        return uuid;
    }

    load(params: ViewParams) {
        var vp: ViewParams = this.ui_nodes.get(params.uuid)!;
        if (vp && vp.node) {
            this.createNode(vp);
        } else {
            cc.resources.load(params.prefabPath, cc.Prefab, (err: Error | null, res: cc.Prefab) => {
                if (err) {
                    cc.error(err);
                }

                let childNode: cc.Node = cc.instantiate(res);
                params.node = childNode;
                //  let comp: DelegateComponent = childNode.addComponent(DelegateComponent);
                //  comp.viewParams = viewParams;
                this.createNode(params);
            });
        }
    }

    createNode(params: ViewParams) {
        params.valid = true;
        // let comp: DelegateComponent = viewParams.node.getComponent(DelegateComponent)!;
        // comp.add();
        params.node.parent = this;
        this.paramsList.push(params)
        return params.node;
    }

    remove(path: string) {
        for (let i = 0; i < this.paramsList.length; i++) {
            let viewParams = this.paramsList[i];
            if (viewParams.prefabPath === path) {
                this.paramsList.splice(i, 1)
                this.ui_nodes.delete(viewParams.uuid);
                viewParams.valid = false;
                viewParams.node.destroy()
            }
        }
    }

    // /**
    // * 根据唯一标识删除节点，如果节点还在队列中也会被删除
    // * @param uuid  唯一标识
    // */
    // protected removeByUuid(uuid: string): void {
    //     var viewParams = this.ui_nodes.get(uuid);
    //     if (viewParams) {
    //         this.ui_nodes.delete(viewParams.uuid);
    //     }
    // }
    clear(): void {
        this.ui_nodes.forEach((value: ViewParams, key: string) => {
            this.remove(value.prefabPath);
            value.valid = false;
        });
        this.ui_nodes.clear();
    }
}